﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnitOfWorkCore.Application.Commands;
using UnitOfWorkCore.Domain.Abstractions;
using UnitOfWorkCore.Domain.Entities;

namespace UnitOfWorkCore.Application
{
    public class DemoApi
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IRepository<User> _userRepository;
        private readonly IRepository<Role> _roleRepository;
        private readonly IMediator _mediator;
        public DemoApi(IRepository<User> userRepository, IRepository<Role> roleRepository, IUnitOfWork unitOfWork, IMediator mediator)
        {
            _userRepository = userRepository;
            _roleRepository = roleRepository;
            _unitOfWork = unitOfWork;
            _mediator = mediator;
        }

        public async Task AddUserRole()
        {
            await _userRepository.InsertSingleData(new User() { UserName = "江北" });
            await _roleRepository.InsertSingleData(new Role { RoleName = "CEO" });
            await _unitOfWork.SaveChangesAsync();
        }

        public async Task AddUser()
        {
            await _mediator.Send(new CreateUserCommand("江北"));
        }

        public async Task<List<User>> QueryUser()
        {
            return await _userRepository.QueryTableList(m => true);
        }
    }
}
